package health

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/response"
	"gitee.com/g-qs/gin-vue-admin/server/model/health"
	healthReq "gitee.com/g-qs/gin-vue-admin/server/model/health/request"
	"gitee.com/g-qs/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type PhysiqueApi struct {
}

var physiqueService = service.ServiceGroupApp.HealthServiceGroup.PhysiqueService

// CreatePhysique 创建Physique
// @Tags Physique
// @Summary 创建Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Physique true "创建Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /physique/createPhysique [post]
func (physiqueApi *PhysiqueApi) CreatePhysique(c *gin.Context) {
	var physique health.Physique
	err := c.ShouldBindJSON(&physique)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.CreatePhysique(&physique); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeletePhysique 删除Physique
// @Tags Physique
// @Summary 删除Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Physique true "删除Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /physique/deletePhysique [delete]
func (physiqueApi *PhysiqueApi) DeletePhysique(c *gin.Context) {
	var physique health.Physique
	err := c.ShouldBindJSON(&physique)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.DeletePhysique(physique); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeletePhysiqueByIds 批量删除Physique
// @Tags Physique
// @Summary 批量删除Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /physique/deletePhysiqueByIds [delete]
func (physiqueApi *PhysiqueApi) DeletePhysiqueByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.DeletePhysiqueByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdatePhysique 更新Physique
// @Tags Physique
// @Summary 更新Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.Physique true "更新Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /physique/updatePhysique [put]
func (physiqueApi *PhysiqueApi) UpdatePhysique(c *gin.Context) {
	var physique health.Physique
	err := c.ShouldBindJSON(&physique)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.UpdatePhysique(physique); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindPhysique 用id查询Physique
// @Tags Physique
// @Summary 用id查询Physique
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query health.Physique true "用id查询Physique"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /physique/findPhysique [get]
func (physiqueApi *PhysiqueApi) FindPhysique(c *gin.Context) {
	var physique health.Physique
	err := c.ShouldBindQuery(&physique)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rephysique, err := physiqueService.GetPhysique(physique.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rephysique": rephysique}, c)
	}
}

// GetPhysiqueList 分页获取Physique列表
// @Tags Physique
// @Summary 分页获取Physique列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.PhysiqueSearch true "分页获取Physique列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /physique/getPhysiqueList [get]
func (physiqueApi *PhysiqueApi) GetPhysiqueList(c *gin.Context) {
	var pageInfo healthReq.PhysiqueSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := physiqueService.GetPhysiqueInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (physiqueApi *PhysiqueApi) SetPhysiqueGraphic(c *gin.Context) {
	var req healthReq.PhysiqueGraphicReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := physiqueService.SetPhysiqueGraphic(req); err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
	} else {
		response.OkWithMessage("设置成功", c)
	}
}

func (physiqueApi *PhysiqueApi) GetPhysiqueGraphic(c *gin.Context) {
	physique_id := c.Query("physique_id")
	if res, err := physiqueService.GetPhysiqueGraphic(physique_id); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(res, c)
	}
}
